package com.pro.staffmanage.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

/**
 * @Author: Haotian Cui
 * @Date: 2022/9/2 - 09 - 02 - 11:57
 * @Description: com.pro.staffmanage.config
 * @Project: staffmanage
 * @version: 1.0
 */
@Configuration
public class MySwaggerConfiguration {

    /**
     * Docket的默认配置都是直接给字段值的
     * 这个 Environment 是org.springframework.core的，用来获取当前运行环境
     */
    @Bean
    @Autowired
    public Docket docketBean(Environment environment){
        return new Docket(DocumentationType.OAS_30)
                //  enable是默认为 true ，如果设置false，那再去打开 swagger-ui 就会在页面提示不可用
                //  acceptsProfiles判断当前环境是什么，根据application.properties/yml/yaml，
                .enable(environment.acceptsProfiles(Profiles.of("dev","test")))
                .groupName("bulabula")
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.pro.staffmanage.controller")
                        //  不是短路或，优先可查看项，多的只做补充
                        .or(RequestHandlerSelectors.withClassAnnotation(RestController.class)
                                .or(RequestHandlerSelectors.withMethodAnnotation(ResponseBody.class))
                                .or(RequestHandlerSelectors.none()))
                )
                .paths(PathSelectors.ant("/**"))
                .build();
    }

    /**
     * 配置需要的 swagger 信息，apiinfo
     *
     * @return
     */
    private ApiInfo apiInfo(){
        //  其中的值会被响应到页面中
        //  作者信息
        Contact contact = new Contact("Haotian Cui", "https://cn.bing.com/", "bulabula@163.com");
        return new ApiInfo(
                "Api Documentation",
                "Api Documentation",
                "1.0",
                "https://www.baidu.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}
